package tech.mobi.child;

import java.util.ArrayList;

import tech.mobi.child.adapter.GridImageAdapter;
import tech.mobi.child.base.BaseAct;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class CZDAClientListPicAddMainAct extends BaseAct implements
		OnClickListener {

	private Button btnBack;
	private Button btnUpload;

	private TextView tvTitle;
	private ProgressBar progress;

	private EditText etDesc;
	private GridView myGrid;
	private TextView tvTypeName;
	private ImageView ivIsQuality;
	private boolean boolQuality = false;
	
	private ArrayList<String> dataList = new ArrayList<String>();
	private GridImageAdapter gridImageAdapter;

	/*****************************
	 * 成长档案类别 flag=1:我们的一家; flag=2:第一天上学; flag=3:精彩的瞬间; flag=5:舞台上的我;
	 * flag=7:得意的作品; flag=9:欢乐的课堂; flag=10:我的好朋友;
	 *****************************/
	private String strTypeId = "";
	private String strTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.czda_client_pic_add_main);

		progress = (ProgressBar) this.findViewById(R.id.progress);
		tvTitle = (TextView) this.findViewById(R.id.tvTitle);
		btnBack = (Button) this.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);
		btnUpload = (Button) this.findViewById(R.id.btnUpload);
		btnUpload.setOnClickListener(this);
		
		etDesc = (EditText) this.findViewById(R.id.etDesc);
		myGrid = (GridView) this.findViewById(R.id.myGrid);
		tvTypeName = (TextView) this.findViewById(R.id.tvTypeName);
		ivIsQuality = (ImageView) this.findViewById(R.id.ivIsQuality);
		ivIsQuality.setOnClickListener(this);

		
		Intent intent = this.getIntent();
		if (intent != null && !"".equals(intent.getStringExtra("typeId"))) {
			strTypeId = intent.getStringExtra("typeId");
		}
		if (intent != null && !"".equals(intent.getStringExtra("titleName"))) {
			strTitle = intent.getStringExtra("titleName");
			tvTitle.setText(getText(Integer.parseInt(strTitle)));

		} else {
			tvTitle.setText("");
		}

		myGrid = (GridView) findViewById(R.id.myGrid);
		dataList.add("camera_default");
		gridImageAdapter = new GridImageAdapter(this, dataList);
		myGrid.setAdapter(gridImageAdapter);
		initListener();
		
		tvTypeName.setText(getText(Integer.parseInt(strTitle)));
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnBack) {
			this.finish();
		} else if(v.getId() == R.id.btnUpload) {
			String s = "";
			for(int i=0; i<dataList.size(); i++) {
				s += dataList.get(i) + ",";
			}
			
			Toast.makeText(this, s, Toast.LENGTH_LONG).show();
		} else if(v.getId() == R.id.ivIsQuality) {
			if(boolQuality) {
				boolQuality = false;
				ivIsQuality.setBackgroundResource(R.drawable.checkbox_nor);
			} else {
				boolQuality = true;
				ivIsQuality.setBackgroundResource(R.drawable.checkbox_checked);
			}
				
		}
	}

	@Override
	public void init() {

	}

	@Override
	public void refresh(Object... param) {

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void initListener() {

		myGrid.setOnItemClickListener(new GridView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// if (position == dataList.size() - 1) {

				Intent intent = new Intent(CZDAClientListPicAddMainAct.this,
						AlbumActivity.class);
				Bundle bundle = new Bundle();
				// intent.putArrayListExtra("dataList", dataList);
				bundle.putStringArrayList("dataList",
						getIntentArrayList(dataList));
				intent.putExtras(bundle);
				startActivityForResult(intent, 0);

				// }

			}

		});
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				ArrayList<String> tDataList = (ArrayList<String>) bundle
						.getSerializable("dataList");
				if (tDataList != null) {
					if (tDataList.size() < 8) {
						tDataList.add("camera_default");
					}
					dataList.clear();
					dataList.addAll(tDataList);
					gridImageAdapter.notifyDataSetChanged();
				}
			}
		}

	}

	private ArrayList<String> getIntentArrayList(ArrayList<String> dataList) {

		ArrayList<String> tDataList = new ArrayList<String>();

		for (String s : dataList) {
			if (!s.contains("default")) {
				tDataList.add(s);
			}
		}

		return tDataList;

	}
}
